import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../config/themes/app_themes.dart';

/// 主题服务
/// 负责处理应用主题相关功能
class ThemeService extends GetxService {
  // 当前主题模式
  Rx<ThemeMode> _themeMode = ThemeMode.system.obs;
  ThemeMode get themeMode => _themeMode.value;
  
  // 当前主题数据
  late ThemeData _theme;
  ThemeData get theme => _theme;
  
  @override
  void onInit() {
    super.onInit();
    
    // 初始化主题
    _initTheme();
  }
  
  /// 初始化主题
  void _initTheme() {
    // 默认使用亮色主题
    _theme = AppThemes.lightTheme;
    
    // 根据系统设置决定使用哪种主题
    final brightness = Get.mediaQuery.platformBrightness;
    if (_themeMode.value == ThemeMode.system) {
      _updateThemeData(brightness == Brightness.dark);
    } else {
      _updateThemeData(_themeMode.value == ThemeMode.dark);
    }
  }
  
  /// 更新主题模式
  void updateThemeMode(ThemeMode mode) {
    _themeMode.value = mode;
    
    if (mode == ThemeMode.system) {
      final brightness = Get.mediaQuery.platformBrightness;
      _updateThemeData(brightness == Brightness.dark);
    } else {
      _updateThemeData(mode == ThemeMode.dark);
    }
  }
  
  /// 更新主题数据
  void _updateThemeData(bool isDark) {
    _theme = isDark ? AppThemes.darkTheme : AppThemes.lightTheme;
    Get.changeTheme(_theme);
  }
  
  /// 切换明暗主题
  void toggleTheme() {
    final isDark = _themeMode.value == ThemeMode.dark;
    updateThemeMode(isDark ? ThemeMode.light : ThemeMode.dark);
  }
  
  /// 获取主题颜色
  Color getColor(String colorName) {
    switch (colorName) {
      case 'primary':
        return _theme.primaryColor;
      case 'secondary':
        return _theme.colorScheme.secondary;
      case 'background':
        return _theme.scaffoldBackgroundColor;
      case 'surface':
        return _theme.cardColor;
      case 'error':
        return _theme.colorScheme.error;
      default:
        return _theme.primaryColor;
    }
  }
} 